﻿/**
 * Description: 解压缩工具类
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2024.11.13
 **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrotliSharpLib;
using System.IO.Compression;
using System.IO;
using System.Net;

namespace ReportBusiness.Tools
{
    public class EncryptHelper
    {
        public static string Encode(string str)
        {
            var bytes = System.Text.Encoding.UTF8.GetBytes(str);
            using (var memoryStream = new MemoryStream())
            {
                using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Compress, true))
                {
                    brotliStream.Write(bytes, 0, bytes.Length);
                }

                var result = Convert.ToBase64String(memoryStream.ToArray());
                return result;
            }
        }

        public static string DeEncode(string str)
        {
            var bytes = Convert.FromBase64String(str);
            using (var memoryStream = new MemoryStream(bytes))
            {
                using (var outputStream = new MemoryStream())
                {
                    using (var decompressStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
                    {
                        decompressStream.CopyTo(outputStream);
                    }
                    var buffer = outputStream.ToArray();
                    return System.Text.Encoding.UTF8.GetString(buffer);
                }
            }
        }

        public static void CompressFile(string inputFilePath, string outputFilePath)
        {
            byte[] fileData = File.ReadAllBytes(inputFilePath);
            using (var memoryStream = new MemoryStream())
            {
                using (var brotliStream = new BrotliStream(memoryStream, CompressionMode.Compress, true))
                {
                    brotliStream.Write(fileData, 0, fileData.Length);
                }

                var buffer = memoryStream.ToArray();
                File.WriteAllBytes(outputFilePath, buffer);
            }
        }

        public static void DecompressFile(string inputFilePath, string outputFilePath)
        {
            byte[] compressedData = File.ReadAllBytes(inputFilePath);
            using (var memoryStream = new MemoryStream(compressedData))
            {
                using (var outputStream = new MemoryStream())
                {
                    using (var decompressStream = new BrotliStream(memoryStream, CompressionMode.Decompress))
                    {
                        decompressStream.CopyTo(outputStream);
                    }
                    var buffer = outputStream.ToArray();
                    File.WriteAllBytes(outputFilePath, buffer);
                }
            }
        }

        //public byte[] CompressJson(string jsonString)
        //{
        //    byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonString);
        //    using (MemoryStream memoryStream = new MemoryStream())
        //    {
        //        using (GZipStream gzipStream = new GZipStream(memoryStream, CompressionMode.Compress))
        //        {
        //            gzipStream.Write(jsonBytes, 0, jsonBytes.Length);
        //        }
        //        return memoryStream.ToArray();
        //    }
        //}

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="text">原字符串</param>
        /// <returns>压缩后的字符串</returns>
        public static string CompressString(string text)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            using (var memoryStream = new MemoryStream())
            {
                using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
                {
                    gzipStream.Write(buffer, 0, buffer.Length);
                }
                return Convert.ToBase64String(memoryStream.ToArray());
            }
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="compressedText">原字符串</param>
        /// <returns>解压后的字符串</returns>
        public static string DecompressString(string compressedText)
        {
            byte[] gzipBuffer = Convert.FromBase64String(compressedText);
            using (var memoryStream = new MemoryStream(gzipBuffer))
            {
                using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    using (var reader = new StreamReader(gzipStream))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }

        static void DownloadFile(string url, string localPath)
        {
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.DownloadFile(url, localPath);
                    Console.WriteLine("文件已成功下载到： " + localPath);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("下载文件时发生错误： " + ex.Message);
            }
        }
    }
}
